package com.topvision.myviewtest.kotlin.part1

/**
 *
 * @author lucy
 * @date 2018-05-30 17:29
 * @description //基本类的写法
 */

fun main(args: Array<String>) {
    val rectangle = Rectangle(10.0, 20.0)
    val triangle = Triangle(3.0, 4.0, 5.0)
    println("rectangle isSquare ${rectangle.isSquare} , rectangle.calculateArea ${rectangle.calculateArea()}")
    println("triangle.calculateArea ${triangle.calculateArea()}")
//out: rectangle isSquare false , rectangle.calculateArea 200.0
//     triangle.calculateArea 6.0
}

// 基类，包含以下计算面积、周长的函数
abstract class Shape(var sides: List<Double>) {
    // 计算周长
    val perimeter: Double get() = sides.sum()

    // 计算面积
    abstract fun calculateArea(): Double
}

// 是否是正方形的接口回调
interface RectangleProperties {
    // 是否是正方形
    val isSquare: Boolean
}


class Rectangle(var height: Double, var width: Double) : Shape(listOf(height, width, height, width)), RectangleProperties {
    override fun calculateArea(): Double {
        return height * width
    }

    override val isSquare: Boolean
        get() = (height == width)
}

class Triangle(val sideA: Double, val sideB: Double, val sideC: Double) : Shape(listOf(sideA, sideB, sideC)) {
    override fun calculateArea(): Double {
        val s = perimeter / 2
        return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC))
    }
}